<?php

/**
 * This is the model class for table "dat_immagini".
 *
 * The followings are the available columns in table 'dat_immagini':
 * @property integer $id
 * @property integer $idGalleria
 * @property string $title
 * @property string $description
 * @property integer $FilesID
 * @property string $alt
 *
 * The followings are the available model relations:
 * @property DatGallerie $galleria
 * @property Files $files
 */
class Immagine extends CActiveRecord {

    public $imageFile;
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Immagine the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'dat_immagini';
    }

    public static function NewImage($destination,$file)
    {
            $Image = new Immagine();
            $Image->title = $file->name;
            $Image->alt = $file->name;
            $Image->files = new FileInfos();             
            $Image->files->FilesName = $destination."/".$file->name;
            $Image->files->Thumbnails = $destination."/thumb_".$file->name;
            //use thumbnailer extension or similar.
            if(!$Image->files->save())
                //return -1;
                throw new CException("Files not saved");
            $Image->FilesID = $Image->files->FilesID;
            
            if(!$Image->save())
                throw new CException("Image not saved  idBanner ".$Image->FilesID."  ".$file->name);            
            $file->saveAs($destination."/".$file->name);
            FileInfos::model()->createThumb($Image->files->Thumbnails,$Image->files->FilesName);            
            return $Image->id;
    }
    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('idGalleria, FilesID', 'numerical', 'integerOnly' => true),
            array('title, description, alt', 'length', 'max' => 1500),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, idGalleria, title, description, FilesID, alt', 'safe', 'on' => 'search'),
            array('imageFile','file','types' => 'jpg, gif, png, jpeg','allowEmpty'=>true),
            array('title', 'required'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'galleria' => array(self::BELONGS_TO, 'Galleria', 'idGalleria'),
            'files' => array(self::BELONGS_TO, 'FileInfos', 'FilesID'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'idGalleria' => 'Galleria',
            'title' => 'Title',
            'description' => 'Description',
            'FilesID' => 'File',
            'alt' => 'Alt',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('idGalleria', $this->idGalleria);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('description', $this->description, true);
        $criteria->compare('FilesID', $this->FilesID);
        $criteria->compare('alt', $this->alt, true);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }

    public function deleteCascade() {
        if ($this->FilesID > 0) {
            //$file = FileInfos::model()->findByPk($this->FilesID);
            $this->files->deleteCascade();
        }
        $this->deleteByPk($this->id);
    }

}